package cn.edu.swu.online;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.Writer;

@WebServlet("/onlineServlet")
public class onlineServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        int online = (int) context.getAttribute("online");
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(online);
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        try (Writer writer = resp.getWriter()) {
            writer.write(json);
        }
    }
}
